function broadcast(componentName, eventName, params) {
  // 把当前this的子组件循环
  this.$children.forEach(child => {
    // name
    const name = child.$options.name;
    if (name === componentName) {
      // name相等时，直接挂载$emit 前面加上$emit的函数,call后就是两参数 eventName监听名 params回调函数的参数
      // (child.$emit).apply(child, [eventName].concat(params));
      (child.$emit).call(child, eventName, params);
    } else {
      // apply和call相当于执行了broadcast函数，在child上执行
      // broadcast.apply(child, [componentName, eventName].concat([params]));
      broadcast.call(child, componentName, eventName, params);
    }
  });
}

module.exports = {
  methods: {
    dispatch(componentName, eventName, params) {
      let parent = this.$parent || this.$root;
      let name = parent.$options.name;
      // while会一直循环
      while (parent && (!name || name !== componentName)) {
        parent = parent.$parent;

        if (parent) {
          name = parent.$options.name;
        }
      }
      if (parent) {
        parent.$emit.apply(parent, [eventName].concat(params));
      }
    },
    broadcast(componentName, eventName, params) {
      // 道理一样，把broadcast函数挂载到this上执行该方法
      broadcast.call(this, componentName, eventName, params);
      // broadcast(componentName, eventName, params)
    }
  }
};